package com.finance.logistics.anlyzier;


import com.finance.base.constant.BaseConstants;
import com.finance.framework.lang.Privilege;
import com.finance.report.bean.pojo.LogisticsData;
import com.finance.system.bean.pojo.Organization;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 货物实际配送站点解析类
 * Created by CBWL on 2017/4/27.
 */
@Component
public class DeliverySiteHandler extends AbstractHandler {

    @Override
    @Resource(name="acceptOrganizationHandler")
    public void setNextHandler(AbstractHandler nextHandler){
        super.setNextHandler(nextHandler);
    }

    @Override
    public void doHandler(LogisticsData logisticsData) {
        String deliverySiteCode = logisticsData.getOrgcodeDelivery();
        Organization organization = this.organizationService.getByCode(deliverySiteCode);
         if(organization == null){
            this.logger.error("未知的运单配送站[deliverySiteCode]:{}",deliverySiteCode);
            String errorReason = "未知的运单配送站[deliverySiteCode]:{"+deliverySiteCode+"}";
            this.logisticsErrorLogService.addErrorLog(logisticsData,errorReason,BaseConstants.ANALYZE_ERROR_TYPE_DELIVERYSITE);
        }else{
             String tableName = this.logisticsDataService.getDataTableName(logisticsData.getSignTime());
             LogisticsData logisticsData2update = this.logisticsDataService.getByWaybillNum(tableName,logisticsData.getWaybillNum());
             logisticsData2update.setOrgcodeDelivery(deliverySiteCode);
             try {
                 this.logisticsDataService.update(logisticsData2update);
                 this.updateDataVild(logisticsData2update,Privilege.B6);
                 this.getNextHandler().doHandler(logisticsData);
             } catch (Exception e) {
                 logger.error(e.getMessage(),e);
             }
        }
    }
}
